package com.cn.yugan.Engine.Adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.cn.yugan.Engine.Beans.MessageBeans;
import com.cn.yugan.mobile.R;

import java.util.List;

/**
 * Created by Rover on 2017/1/5 .
 * Email 357319517@qq.com
 */
public class ChatMsgViewAdapter extends BaseAdapter {

    public static interface IMsgViewType {
        int IMVT_COM_MSG = 0;// 收到对方的消息  
        int IMVT_TO_MSG = 1;// 自己发送出去的消息  
    }

    private static final int ITEMCOUNT = 2;// 消息类型的总数  
    private List<MessageBeans> coll;// 消息对象数组  
    private LayoutInflater mInflater;
    private String username;

    public ChatMsgViewAdapter(Context context, List<MessageBeans> coll, String username) {
        this.coll = coll;
        if ("".equals(username)) {
            this.username = "系统信息";
        } else {
            this.username = username;
        }
        mInflater = LayoutInflater.from(context);
    }

    public int getCount() {
        return coll.size();
    }

    public Object getItem(int position) {
        return coll.get(position);
    }

    public long getItemId(int position) {
        return position;
    }

    /**
     * 得到Item的类型，是对方发过来的消息，还是自己发送出去的
     */
    public int getItemViewType(int position) {
        MessageBeans entity = coll.get(position);

        if (!entity.isend()) {//收到的消息  
            return IMsgViewType.IMVT_COM_MSG;
        } else {//自己发送的消息  
            return IMsgViewType.IMVT_TO_MSG;
        }
    }

    /**
     * Item类型的总数
     */
    public int getViewTypeCount() {
        return ITEMCOUNT;
    }

    public View getView(int position, View convertView, ViewGroup parent) {

        MessageBeans entity = coll.get(position);
        boolean isend = entity.isend();

        ViewHolder viewHolder = null;
        if (convertView == null) {
            if (!isend) {
                convertView = mInflater.inflate(
                        R.layout.chatting_item_msg_text_left, null);
            } else {
                convertView = mInflater.inflate(
                        R.layout.chatting_item_msg_text_right, null);
            }

            viewHolder = new ViewHolder();
            viewHolder.tvSendTime = (TextView) convertView
                    .findViewById(R.id.tv_sendtime);
            viewHolder.tvUserName = (TextView) convertView
                    .findViewById(R.id.tv_username);
            viewHolder.tvContent = (TextView) convertView
                    .findViewById(R.id.tv_chatcontent);
            viewHolder.isComMsg = isend;
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        viewHolder.tvSendTime.setText(entity.getTime());
        viewHolder.tvUserName.setText(username);
        viewHolder.tvContent.setText(entity.getContent());
        return convertView;
    }

    static class ViewHolder {
        public TextView tvSendTime;
        public TextView tvUserName;
        public TextView tvContent;
        public boolean isComMsg = true;
    }

}  
